﻿using System;
using LeRenard.Libraries.Templating;
using System.IO;

namespace LeRenard.Libraries.Templating.TemplatingExample
{
    class Program
    {
        static void Main(string[] args)
        {
            var message = "METHOD ::{0}:: \n{1}";
            
                ExecuteAndPrint(ApiUsageFromString, message);
                ExecuteAndPrint(ApiUsageForNonGenericVersion, message);
                ExecuteAndPrint(ApiUsageForGenericVersion, message);            

            Console.WriteLine();
            Console.WriteLine("=== [END] ===");
            Console.ReadLine();
        }

        /// <summary>
        /// Helper method.
        /// </summary>        
        private static void ExecuteAndPrint(Func<string> expression, string message)
        {
            Console.WriteLine(message, expression.Method.Name, expression());
            Console.WriteLine();
            Console.WriteLine();
        }

        /// <summary>
        /// Example where we build a template from a string.
        /// </summary>
        private static string ApiUsageFromString()
        {
            var template =
                TemplateFactory.New()
                .FromString("{greeting} {audience}!");

            template
                .Map("{greeting}")
                .To("Hello");

            template
                .Map("{audience}")
                .To("World");

            var personalisedTemplate
                = template.GetProcessedTemplate();

            return personalisedTemplate;
        }

        /// <summary>
        /// Example where we build a template from file, be it a txt, html, css, or whatever 'template' file.
        ///
        /// We then can attach (map)
        /// - fixed (or constant) values to the placeholder;
        /// - functions that return a <see cref="string"/> value.
        ///
        /// We call .GetProcessedTemplate() on the <see cref="ITemplate"/> and receive the filled out template.
        /// </summary>
        private static string ApiUsageForNonGenericVersion()
        {
            var currentDirectory = Directory.GetCurrentDirectory();

            var template =
                TemplateFactory.New()
                .FromFile(currentDirectory + @"\template.html");

            template
                .Map("{naam}")
                .To("Yves Schelpe");

            template
                .Map("{date}")
                .To(() => DateTime.Now.ToShortDateString());

            var personalisedTemplate =
                template.GetProcessedTemplate();

            return personalisedTemplate;
        }

        /// <summary>
        /// Example where we build an TObject dependent template from file, be it a txt, html, css, or whatever 'template' file.
        ///
        /// We then can attach (map)
        /// - fixed (or constant) values to the placeholder;
        /// - functions that return a <see cref="string"/> value;
        /// - functions that take in a TObject, and return a <see cref="string"/> value.
        ///
        /// We call .GetProcessedTemplate() on the <see cref="ITemplate"/> and receive the filled out template.
        /// </summary>
        private static string ApiUsageForGenericVersion()
        {
            var currentDirectory = Directory.GetCurrentDirectory();

            var template =
                TemplateFactory.New<AppDomain>()
                .FromFile(currentDirectory + @"\template.html");

            template
                .Map("{naam}")
                .To(appDomain => appDomain.FriendlyName);

            template
                .Map("{date}")
                .To(() => DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToLongTimeString());

            var personalisedTemplate =
                template.GetProcessedTemplate(AppDomain.CurrentDomain);

            return personalisedTemplate;
        }
    }
}
